// WebSocket
open_im = true;
var supplier_arr = ["193","486","217","202"]
if (open_im == true && supplier_arr.includes(supplier_id) == true) {
	var ws_flag = true;
	var ws_addr = 'wss://tailongji.dixintong.com:2346';
	var ws = {};
	var h = null;
	var s = null;
	var handelMsg = (data) => {
		/* var time = new Date();
		var hours = time.getHours();
		var minutes = time.getMinutes();
		var seconds = time.getSeconds();
		console.log(hours + ":" + minutes + ":" + seconds, data) */
		//console.log(data)
		var msg = JSON.parse(data)
		switch (msg.type) {
			case 'order_deff_time':
				if (msg.warm == true) {
					layer.msg('请注意：已经超过' + Math.floor(msg.deff_time / 60) + '分钟没有收到订单！', {
						offset: 'auto',
						anim: 6,
						time: 0
					})
				} else {
					layer.closeAll('tips');
				}
				break;
		}
	}

	var sotckTask = () => {
		ws = new WebSocket(ws_addr)
		//console.log(ws, '链接')
		ws.onopen = () => {
			/* if (h != null) {
				clearInterval(h)
			} */
			let ping = {
				type: 'ping',
				user_id: supplier_id,
				uid: supplier_id + '_admin',
			}
			s = setInterval(() => {
				// 发送ping
				ws.send(JSON.stringify(ping));
			}, 30000)

			ws.onmessage = (e) => {
				handelMsg(e.data)
			}
		}
		/* ws.onerror = () => {
			console.log('错误,重连')
			h = setInterval(() => {
				sotckTask()
			}, 3000)
		} */

		ws.onclose = () => {
			console.log('断开链接,重连')
			clearInterval(s)
			setTimeout(() => {
				sotckTask()
			}, 3000)
		}
	}

	try {
		sotckTask()
	} catch (e) {
		console.log('错误', e)
		sotckTask()
	}

}